﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Management.Automation;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using Microsoft.SharePoint.Utilities;
using System.Web;


namespace AVO.Layouts.AVO
{
    public partial class avoct : AVOPageBase, IPostBackEventHandler
    {
        private DataTable m_dtCFG = null;
        
        public SPList CurrentList = SPContext.Current.List;
        public string ContentTypeName = null;

        private ContentTypeConfiguration Configuration;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            string strctid = this.Request.QueryString["ctype"];
            var ctid = new SPContentTypeId(strctid);

            var cfg = this[ctid];
            ContentTypeName = cfg.ContentTypeName;

            Configuration = cfg;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeFieldsGrid();

            if (!Page.IsPostBack)
            {
                btnEnable.Enabled = !Configuration.Enabled;
                btnDisable.Enabled = Configuration.Enabled;

                Configuration.Save();
            }
        }

        private void InitializeFieldsGrid()
        {

            fieldsGridMenuTemplateEdit.ClientOnClickNavigateUrl = string.Format(
                "avoeditfield.aspx?ctype={0}&List={1}&fid=%TITLE%&Source={2}",
                strContentTypeID,
                strListId,
                HttpUtility.UrlEncode(this.Request.RawUrl)
                );

            m_dtCFG = new DataTable();
            m_dtCFG.Columns.Add(new DataColumn("Title"));
            m_dtCFG.Columns.Add(new DataColumn("Guid"));
            m_dtCFG.Columns.Add(new DataColumn("Enabled"));
            m_dtCFG.Columns.Add(new DataColumn("CTRequired"));
            m_dtCFG.Columns.Add(new DataColumn("CTReadOnly"));
            m_dtCFG.Columns.Add(new DataColumn("CTSINF"));
            m_dtCFG.Columns.Add(new DataColumn("CTSIEF"));
            m_dtCFG.Columns.Add(new DataColumn("CTSIDF"));
            m_dtCFG.Columns.Add(new DataColumn("ContentType"));

            foreach (var fieldCfg in Configuration.FieldConfigurations)
            {
                DataRow row = m_dtCFG.NewRow();

                row["Title"] = fieldCfg.FieldTitle;
                row["Guid"] = fieldCfg.FieldId;
                row["Enabled"] = fieldCfg.Enabled;
                row["ContentType"] = Configuration.ContentTypeName;

                SPField spf = Configuration.GetSPField(fieldCfg.FieldId);
                row["CTRequired"] = spf.Required;
                row["CTReadOnly"] = spf.ReadOnlyField;
                row["CTSINF"] = spf.ShowInNewForm;
                row["CTSIEF"] = spf.ShowInEditForm;
                row["CTSIDF"] = spf.ShowInDisplayForm;

                m_dtCFG.Rows.Add(row);
            }


            fieldsGrid.DataSource = m_dtCFG;
            fieldsGrid.DataBind();
        }

        public void RaisePostBackEvent(string eventArgument)
        {
        }

        public void btnEnable_Click(object sender, EventArgs ea)
        {
            Configuration.Enabled = true;
            Configuration.Save();
        }
        public void btnDisable_Click(object sender, EventArgs ea)
        {
            Configuration.Enabled = false;
            Configuration.Save();
        }
    }
}
